package com.xiaoshuidi.cloud.module.rooms.valid;


import com.xiaoshuidi.cloud.module.rooms.annotation.ExcelValid;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomhouse.vo.WholeLeaseTemplateVo;

import java.lang.reflect.Field;
import java.util.Map;
import java.util.Objects;
/**
 * 整租的导入校验
 * @Author Summer
 * @date 2023/5/22
 */
public class WholeLeaseImportValid extends AbstractHouseExcelImportValid<WholeLeaseTemplateVo> {
    @Override
    public boolean valid(WholeLeaseTemplateVo whole, Map<String, String> errorMap) throws Exception {
        Field[] fields = whole.getClass().getDeclaredFields();
        //总楼层
        Integer num = 0;
        //所在楼层
        Integer floor = 0;
        for (Field field : fields) {
            field.setAccessible(true);
            //属性的值
            Object fieldValue = null;
            String fieldName = field.getName();
            try {
                fieldValue = field.get(whole);
            } catch (IllegalAccessException e) {
                errorMap.put("exception", field.getAnnotation(ExcelValid.class).message());
                return false;

            }
            boolean isValid = field.isAnnotationPresent(ExcelValid.class);
            if (isValid && Objects.isNull(fieldValue)) {
                errorMap.put("exception", field.getAnnotation(ExcelValid.class).message());
                return false;
            }
            //出租形式
            if (fieldName.equals("leaseType")) {
                String leaseType = (String) fieldValue;
                if (!leaseType.equals("整租")) {
                    errorMap.put(fieldName, "出租形式录入有误！");
                    return false;
                }

            }
            if (fieldName.equals("num")) {
                num = (Integer) fieldValue;
            }
            if (fieldName.equals("floor")) {
                floor = (Integer) fieldValue;
            }

            boolean result = validHouseImportItem(fieldName, fieldValue, errorMap);
            if (!result){
                return  result;
            }
        }
        //所在楼层不可以大于总楼层
        if (floor > num) {
            errorMap.put("floor", "所在楼层输入有误！");
            return false;
        }


        return true;
    }
}
